package com.wude.guild.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.PointerIcon;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.mc.developmentkit.utils.MCUtils;
import com.mc.developmentkit.utils.ToastUtil;
import com.wude.bean.CommentariesBean;
import com.wude.bean.DoubleDialogBean;
import com.wude.bean.JifenStoreBean;
import com.wude.guild.R;
import com.wude.guild.dialog.DoubleButtonDialog;
import com.wude.guild.face.DoubleButtonFace;
import com.wude.http.HttpCom;
import com.wude.tools.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 唐小妹 on 2017/12/12 0012.
 * 积分商城
 */

public class JifenStoreAdapter extends RecyclerView.Adapter<JifenStoreAdapter.JifenStoreItem> implements DoubleButtonFace {
    private List<JifenStoreBean> datas = new ArrayList<>();
    private Context context;
    private DoubleButtonDialog dialog;
    private JifenStoreBean jifenBean;

    public JifenStoreAdapter(Context context) {
        this.context = context;
    }

    @Override
    public JifenStoreItem onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.jifen_store_item, parent, false);
        return new JifenStoreItem(view);
    }

    @Override
    public void onBindViewHolder(JifenStoreItem holder, int position) {
        final JifenStoreBean jifenStoreBean = datas.get(position);
        MCUtils.fillImage(holder.img, jifenStoreBean.cover);
        holder.tv_goodName.setText(jifenStoreBean.good_name);
        holder.tv_price.setText(jifenStoreBean.price + "");
        holder.tv_exchange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DoubleDialogBean bean = new DoubleDialogBean();
                bean.setDesc(jifenStoreBean.good_usage);
                jifenBean = jifenStoreBean;
                bean.setLeftButton("取消");
                bean.setRightButton("确定");
                bean.setTitle(jifenStoreBean.good_name);
                dialog = new DoubleButtonDialog(context, bean, JifenStoreAdapter.this);
                dialog.show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    /**
     * 左边按钮
     */
    @Override
    public void kown() {
        dialog.dismiss();
    }

    /**
     * 右边按钮
     */
    @Override
    public void goon() {
        //兑换逻辑
        Map<String, String> map = new HashMap<>();
        map.put("token", Utils.getLoginUser().token);
        map.put("id", jifenBean.id + "");
        HttpCom.POST(handler, HttpCom.getGetPointShop(), map, false);
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    dialog.dismiss();
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        ToastUtil.showToast(jsonObject.getString("return_code"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络错误");
                    break;
            }
        }
    };

    public static class JifenStoreItem extends RecyclerView.ViewHolder {
        ImageView img;
        TextView tv_goodName;
        TextView tv_price;
        TextView tv_exchange;

        public JifenStoreItem(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.img);
            tv_goodName = itemView.findViewById(R.id.tv_goodName);
            tv_price = itemView.findViewById(R.id.tv_price);
            tv_exchange = itemView.findViewById(R.id.tv_exchange);
        }

    }

    public void setDataList(List<JifenStoreBean> dataList) {
        this.datas = dataList;
        notifyDataSetChanged();
    }
}
